// const { title } = require("process")

var app=getApp()
Page({
  data: {
    itext:0,
    windowheight:0,
    containerheight:0,
    containerbottomheight:0,
    keyboardheight:0,
    iphone:false,
    delshow:1,
    openid:"",
    id:"",
    _id:"",
    d_title:"",
    zone:0,
    content:"",
    reward:"",
    imageid:"",
    imageurl:"",
    comment:"",
    ishide:true,
    ishide2:true,
    username:"",
    myDate:"",
    fabuzhi:""
  },
  load_data()
  {
    wx.showLoading({
      title: '加载中...',
      mask:true
    })
    let that=this
    wx.cloud.callFunction({
      name:"getMessage",
      data:{
        id:that.data._id
      },
      success(res){
        console.log("数据上传成功",res)
        that.setData({
          id:res.result.data["userid"],
          title:res.result.data["title"],
          content:res.result.data['content'],
          comment:res.result.data['comment'],
          imageurl:res.result.data['fileID'],
          reward:res.result.data['reward'],
          zone:res.result.data['zone'],
          img:res.result.data["img"],
          username:res.result.data["username"],
          myDate:res.result.data["myDate"]
        })
        if(that.data.imageurl!='')
        {
          that.setData({
            ishide:false
          })
        }
        if(that.data.reward!='')
        {
          that.setData({
            ishide2:false
          })
        }
        wx.hideLoading()
     },
     fail(res){
      console.log("数据上传失败",res)
     }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var t=this
    wx.getSystemInfo({
      success: (result) => {t.data.windowheight=result.windowHeight
      t.data.iphone=result.model.indexOf("iPhone")>=0},
    })
    this.setData({
      _id:options._id
    })
    this.load_data()

    

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
     var t=this
     setTimeout(() => {
       t.refreshcontainerheight()
     }, 500)

  },
 refreshcontainerheight(){
      var t=this
      var query=wx.createSelectorQuery();
      query.select(".container").boundingClientRect()
      query.exec((result)=>{
        t.data.containerheight=result[0].height;
        t.data.containerbottomheight=result[0].bottom
      })
 },
  /**
   * 生命周期函数--监听页面显示
   */
  onPageScroll:function(){
       var t=this
       t.refreshcontainerheight()

  },


  focus:function(e){
    var t=this
    if(t.data.iphone!=true)
    {
      var keyboardheight=e.detail.height
      var windowheight=t.data.windowheight
      var containerheight=t.data.containerheight
      var containerbottomheight=t.data.containerheight
      if(containerheight>windowheight){
      if(containerbottomheight-windowheight>keyboardheight)
      {
         t.setData({
           keyboardheight:e.detai.height
         })
      }
      else{
        t.setData({
          keyboardheight:containerbottomheight-windowheight
        })
      }
    }
    else{
      t.setData({
        keyboardheight:0
      })
    }
    }
    else{
      t.setData({
        keyboardheight:0
      })
    }
  },

  blur:function(e){
      var t=this
      t.setData({
        keyboardheight:0
      })

},

  clickinput:function(e)
{
    this.setData({
        itext:e.detail.value
    })
},


click()
{
  wx.showLoading({
    title: '发布中...',
    mask:true
  })
  let that=this
  var timestamp = Date.parse(new Date());
  var date = new Date(timestamp);
  //获取年份  
  var Y =date.getFullYear();
  //获取月份  
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
  //获取当日日期 
  var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(); 
  var H=date.getHours()<10? '0'+date.getHours():date.getHours()
  var Mi=date.getMinutes()<10? '0'+date.getMinutes():date.getMinutes()
  var myDate={
    'Y':Y,
    'M':M,
    'D':D,
    'H':H,
    'Mi':Mi
  }
  // var gg='comment.'+app.globalData.openid
  // this.setData({
  //   [gg]:[app.globalData.username,this.data.itext]
  // })
  var obj=[app.globalData.username,this.data.itext,app.globalData.openid,myDate,app.globalData.img]
  this.data.comment.push(obj)
    this.setData({
    comment:this.data.comment
  })
  this.setData({
    fabuzhi:""
  })
  wx.cloud.callFunction({
    name:"addComments",
    data:{
         id:this.data._id,
         openid:that.data.id,
         title:that.data.title,
         content:that.data.content,
         zone:that.data.zone,
         comment_openid:app.globalData.openid,
         comment:this.data.comment,
         MyDate:myDate
    },
    success(res){
      console.log(myDate)
      console.log("数据上传成功",res)
      wx.hideLoading()
      wx.showToast({
        title: '发布成功',
        icon: 'success',
        duration: 1500
      })
      
   },
   fail(res){
    console.log("数据上传失败",res)
   }
  })
  
},

toMyPage(){
  let that=this
  console.log(that.data.id)
  wx.navigateTo({
    url: '../myPage/myPage?openid='+that.data.id,
  })
},
toMyPages(e){
  let that=this
  console.log(that.data.id)
  wx.navigateTo({
    url: '../myPage/myPage?openid='+that.data.comment[e.currentTarget.dataset.index][2],
  })
},
clickImg: function(e){
  var imgUrl = this.data.imageurl;
  wx.previewImage({
    urls: [imgUrl], //需要预览的图片http链接列表，注意是数组
    current: '', // 当前显示图片的http链接，默认是第一个
  })
}

})